let express=require("express")();
let expressStatic=require("express-static");
let mysql=require("mysql");
const port=81;
//连接数据库
let sql=mysql.createConnection({
	host:'localhost',
	user:'root',
	password:'123456',
	database:'goods'
});
sql.connect();
//操作列表页数据库
express.get("/getlist",(request,response)=>{
	sql.query(`SELECT * FROM goods`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("请求失败");
		}else{
			console.log(data);
			response.send(data);
		}
	})
})
//操作详情页数据库
express.get("/details",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT * FROM goods WHERE id="${id}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("请求失败");
		}else{
			console.log(data);
			response.send(data[0]);
		}
	})
})
express.get("/details2",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT * FROM details2`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("请求失败");
		}else{
			console.log(data);
			response.send(data);
		}
	})
})

//注册部分
express.get("/addUser",(request,response)=>{
	sql.query(`INSERT INTO users (phone,tucode,message,paws,name,urename,username) VALUE ("${request.query.phone}","${request.query.tucode}","${request.query.message}","${request.query.paws}","${request.query.name}","${request.query.urename}","${request.query.username}")`,(error,data)=>{
		if(error){
			response.send("error");
			console.log(error);
		}else{
			response.send("success!");
			console.log(`${request.query.username}用户增加成功!`)
		}
	})
})
//密码登录部分
 express.get("/load",(request,response)=>{
	 // console.log("a"+request.query.phone)
	 // console.log("b"+request.query.paws)
	sql.query(`SELECT * FROM users WHERE phone="${request.query.phone}" AND paws="${request.query.paws}"`,(error,data)=>{
		if(error){
			response.send("error")
			console.log(error);
		}else{
			if(data.length==0){
				response.send({errorid:"error"})
				console.log(data)
			}else{
				response.send(data);
				// console.log(data)
				console.log(`${request.query.phone}用户登录成功!`)
			}
			
		}
	})
})
//短信验证码登录部分
express.get("/loadb",(request,response)=>{
	 // console.log("a"+request.query.phone)
	 // console.log("b"+request.query.paws)
	sql.query(`SELECT * FROM users WHERE phone="${request.query.phone}" AND tucode="${request.query.tucode}" AND message="${request.query.message}"`,(error,data)=>{
		if(error){
			response.send("error")
			console.log(error);
		}else{
			if(data.length==0){
				response.send({errorid:"error"})
				console.log(data)
			}else{
				response.send(data);
				// console.log(data)
				console.log(`${request.query.phone}用户登录成功!`)
			}
			
		}
	})
})
//填写地址部分
express.get("/getaddress",(request,response)=>{
	sql.query(`INSERT INTO orders (surname,name,phone,city,address) VALUES("${request.query.surname}","${request.query.name}","${request.query.phone}","${request.query.city}","${request.query.address}")`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("error");
		}else{
			console.log(data)
			response.send("用户信息");
		}
	})
})
//订单页面显示地址
express.get("/getcustomeraddress",(request,response)=>{
	let id=request.query.id;
	sql.query(`SELECT * FROM orders WHERE id="${id}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.send("请求失败");
		}else{
			console.log(data);
			response.send(data);
		}
	})
})

//加入静态页面
express.use(expressStatic(__dirname+"/text"));
//启动服务
express.listen(port);